package cn.cas.mango.service;

import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Service
@Data
public class ConfigService {

  @Value("${elasticsearch.index}")
  private String index;

  @Value("${history.path}")
  private String historyPath;
  @Value("${fragment.size}")
  private Integer fragmentSize;
  @Value("${elasticsearch.host}")
  private String host;
  @Value("${elasticsearch.port}")
  private int port;
  @Value("${elasticsearch.username:-}")
  private String username;
  @Value("${elasticsearch.password:-}")
  private String password;
  @Value("${default.hits.count}")
  private int hitsCount;

  @Value("${boost}")
  private float boost;



  @Value("${aggTimes}")
  private Double aggTimes;
  @Value("${read.batch.size}")
  private int readBatchSize;

  @Value("${cluster.page.size}")
  private int pageSize;


  @Value("${cluster.like.degree}")
  private Double likeDegree;

  @Value("${max.read.size}")
  private Integer maxReadSize;

  @Value("${cluster.default.platform}")
  private String defaultPlatform;

  @Value("${cluster.default.Chinese.ratio}")
  private Double chineseRatio;


  @Value("${cluster.filter.not.Chinese}")
  private boolean clusterFilterNotChinese;


  @Value("${searchHot.default.if.agg}")
  private Boolean defaultIfAgg;

  @Value("${searchHot.default.pageSize}")
  private Integer searchHotPageSize;

  @Value("${searchHot.default.searchMode}")
  private Integer searchMode;

  @Value("${searchHot.default.simWeight}")
  private Double simWeight;

  @Value("#{'${getAssembleHot.no.vector.subjects}'.split(',')}")
  private List<String> getAssembleHotNoVectorSubjects;

  @Value("${searchHot.default.aggWeight}")
  private Double searchHotAggregationWeight;

  @Value("${cluster.keywords}")
  private boolean keywordsCluster;

  @Value("${model.summary.url}")
  private String modelSummaryUrl;


  @Value("${vector.size}")
  private Integer vectorSize;

  @Value("${elasticsearch.alias.index}")
  private String aliasIndex;
  private final Map<Integer, String> colorMap = new HashMap<Integer, String>(){{
    put(1, "#337AB7");
    put(2, "#54BA9A");
    put(3, "#FF7D3D");
    put(4, "#E55050");
  }};

  private List<String> totalSubject = new ArrayList<>();
  {
    List<String> list = Arrays.asList("时政", "财经", "社会", "科技", "体育", "娱乐", "游戏", "时尚", "股票", "房产", "彩票", "家居", "教育", "星座");
    totalSubject.addAll(list);
  }


  private Set<String> blackList = new HashSet<>();
  {
    try {
      blackList.addAll(getBlackList1("blacklist.txt"));
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
  }
  public List<String> getBlackList1(String filePath) throws IOException {
    FileInputStream fis = new FileInputStream(filePath);
    List<String> blackLists = new ArrayList<>();
    //Construct BufferedReader from InputStreamReader
    BufferedReader br = new BufferedReader(new InputStreamReader(fis));
    String line = null;
    while ((line = br.readLine()) != null) {
      blackLists.add(line.trim());
    }
    br.close();
    fis.close();
    return blackLists;
  }

}
